<?php
/////////////////////////////////////////////////////////////////////////////////////		
include "required_code.php"; 

	session_start();
	$ses_user_id =$_SESSION['ses_user_id'];
	$ses_std_id = $_SESSION['ses_std_id'];
	if($ses_user_id <> session_id() or  $ses_std_id ==''){
		echo "Authentication failed";
		echo "<meta http-equiv='refresh' content='2;URL=index.php' />";
		}    
						/*	$query2="SELECT * FROM std_progress WHERE std_id=$ses_std_id";
							$db_result2=db_query($query2);
							$result2=mysql_fetch_array($db_result2);
							//echo $result2['ls_sts'];
							//echo $result2['pretest_sts'];
								if($result2['ls_sts']==1){
								echo "Redirecting";
								echo "<meta http-equiv='refresh' content='1;URL=progress.php' />";
							}*/
							else {
/////////////////////////////////////////////////////////////////////////////////////	
/////////////FIND FULL WEIGHT OF EACH CONCEPT//////////////////
			$query2="SELECT * FROM preweight_concept";
			$db_result2=db_query($query2);
			$c1_weight=0;			$c2_weight=0;			$c3_weight=0;			$c4_weight=0;
			while($result2=mysql_fetch_array($db_result2)){
				$c1_weight+=$result2['C1'];
				$c2_weight+=$result2['C2'];
				$c3_weight+=$result2['C3'];
				$c4_weight+=$result2['C4'];
			}
			/////////////FIND FULL WEIGHT OF EACH CONCEPT//////////////////
			
			
			////////////FIND WEIGHT OF EACH CONCEPT [ONLY WRONG ANS ITEMS]//////////////////
			$query3="SELECT * FROM std_ans WHERE std_id=$ses_std_id AND test_result=0";
			$db_result3=db_query($query3);
			$c1_std_weight=0;			$c2_std_weight=0;			$c3_std_weight=0;			$c4_std_weight=0;

			while($result3=mysql_fetch_array($db_result3)){
				//echo $result3['test_item'];
				$query4="SELECT * FROM preweight_concept WHERE Item_No='".$result3['test_item']."'";
				$db_result4=db_query($query4);
				$result4=mysql_fetch_array($db_result4);
				$c1_std_weight+=$result4['C1'];
				$c2_std_weight+=$result4['C2'];
				$c3_std_weight+=$result4['C3'];
				$c4_std_weight+=$result4['C4'];
			}
			////////////FIND WEIGHT OF EACH CONCEPT [ONLY WRONG ANS ITEMS]//////////////////
			
			////////////FIND ERROR RATIO OF EACH CONCEPT////////////////////
				$err_ratio_c1=$c1_std_weight/$c1_weight;
				$err_ratio_c2=$c2_std_weight/$c2_weight;
				$err_ratio_c3=$c3_std_weight/$c3_weight;
				$err_ratio_c4=$c4_std_weight/$c4_weight;
			////////////FIND ERROR RATIO OF EACH CONCEPT////////////////////
			
			////////////ALL POSSIBLE PATHS////////////////////
			
						//////////////FIND MAXIMUM ERROR RATIO OF PATH1: C1 -> C3 -> C4////////////////
							$p1_max_error_ratio[0]=$err_ratio_c1;
							$p1_max_error_ratio[1]=$err_ratio_c3;
							$p1_max_error_ratio[2]=$err_ratio_c4;
							$p1_max_error_ratio=Maximum($p1_max_error_ratio);
						//////////////FIND MAXIMUM ERROR RATIO OF PATH1: C1 -> C3 -> C4////////////////
			
						//////////////FIND MAXIMUM ERROR RATIO OF PATH2: C3 -> C4////////////////
							$p2_max_error_ratio[0]=$err_ratio_c3;
							$p2_max_error_ratio[1]=$err_ratio_c4;
							$p2_max_error_ratio=Maximum($p2_max_error_ratio);
						//////////////FIND MAXIMUM ERROR RATIO OF PATH2: C3 -> C4////////////////
			
								//////////////FIND MAXIMUM ERROR RATIO OF PATH3: C2 -> C4////////////////
							$p3_max_error_ratio[0]=$err_ratio_c2;
							$p3_max_error_ratio[1]=$err_ratio_c4;
							$p3_max_error_ratio=Maximum($p3_max_error_ratio);
						//////////////FIND MAXIMUM ERROR RATIO OF PATH3: C2 -> C4////////////////
			////////////ALL POSSIBLE PATHS////////////////////
			
			
			
			////////////////FIND CRITACAL PATHS/////////////////////
			$path_max_error_ratio[0]=$p1_max_error_ratio;
			$path_max_error_ratio[1]=$p2_max_error_ratio;
			$path_max_error_ratio[2]=$p3_max_error_ratio;
			$path_max_error_ratio=Maximum($path_max_error_ratio);
			
			//////////////////////////////
			function std_path($p,$ses_std_id){
				$query_select_std_vs_path1="SELECT * FROM std_vs_path WHERE std_id=$ses_std_id AND path=$p";
				$db_result_select_std_vs_path1=db_query($query_select_std_vs_path1);
				$result_select_std_vs_path1=mysql_num_rows($db_result_select_std_vs_path1);
				//echo "<br>";
				if($result_select_std_vs_path1>0){
					$query_del_std_vs_path1="DELETE FROM std_vs_path WHERE std_id=$ses_std_id AND path=$p";
					db_query($query_del_std_vs_path1);
				}
				
					$query_std_vs_path1="INSERT INTO std_vs_path(std_id,path)  VALUES ('".$ses_std_id."','".$p."')";
					db_query($query_std_vs_path1);
			}
			
			function std_c($sc,$ses_std_id){
				  $query_select_std_vs_c1="SELECT * FROM std_vs_concept WHERE std_id=$ses_std_id AND concept=$sc";
				  $db_result_select_std_vs_c1=db_query($query_select_std_vs_c1);
				  $result_select_std_vs_c1=mysql_num_rows($db_result_select_std_vs_c1);
			  
				  if($result_select_std_vs_c1=='0'){
					  $query_std_vs_c1="INSERT INTO std_vs_concept(std_id,concept,status)  VALUES ('".$ses_std_id."','".$sc."','0')";
					  db_query($query_std_vs_c1);
				  }
			}
			
			
			$path1=0;	$path2=0;	$path3=0;
			if($p1_max_error_ratio>=$p2_max_error_ratio && $p1_max_error_ratio>=$p3_max_error_ratio) $path1=1;
			if($p2_max_error_ratio>=$p1_max_error_ratio && $p2_max_error_ratio>=$p3_max_error_ratio) $path2=1;
			if($p3_max_error_ratio>=$p1_max_error_ratio && $p3_max_error_ratio>=$p2_max_error_ratio) $path3=1;
				
				
				if($path1){
					std_path(1,$ses_std_id);
					std_c(1,$ses_std_id);
					std_c(3,$ses_std_id);
					std_c(4,$ses_std_id);
				}
			
				if($path2){
					std_path(2,$ses_std_id);
					std_c(2,$ses_std_id);
					std_c(4,$ses_std_id);
				}
			
				if($path3){
					std_path(3,$ses_std_id);
					std_c(3,$ses_std_id);
					std_c(4,$ses_std_id);			
				}
			////////////////FIND CRITACAL PATHS/////////////////////
			
			///////////////READY TO REDIRECT///////////////
			if($path1 || $path2 || $path3){
				echo "Analysis completed.";
				$_SESSION['path1']=$path1;
				$_SESSION['path2']=$path2;
				$_SESSION['path3']=$path3;
				echo "<meta http-equiv='refresh' content='1;URL=learning_paths.php' />";
			}
			///////////////READY TO REDIRECT///////////////

/////////////////////////////////////////////////////////////////////////////////////		
		$db_close=db_disconnect($db_open);
		}
/////////////////////////////////////////////////////////////////////////////////////		
?>